package com.achcorp.sarch.clients.thick;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

import org.jdesktop.swingx.JXDatePicker;

import com.achcorp.sarch.core.dao.DocumentDAO;
import com.achcorp.sarch.core.domain.DocumentImp;

public class AddFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JComponent[] components;

	public AddFrame(final DocumentDAO docDao,final DocsModel docModel) {
		//super();
/*
		setTitle("AddFrame");
		setSize(450, 350);
		setLocationRelativeTo(null);
		setResizable(true);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		MigLayout migLayout = new MigLayout("wrap 15");

		JPanel mainPan = new JPanel(migLayout);
		components = new JComponent[DocumentImp.COLUMN_COUNT];
		for (int i = 0; i < DocumentImp.COLUMN_COUNT; i++) {

			JLabel jl = new JLabel(DocumentImp.getColumnName(i));
			mainPan.add(jl);

			if (DocumentImp.getColumnClass(i).equals(Date.class)) {
				JXDatePicker datePicker = new JXDatePicker(new Date());
				components[i] = datePicker;
				mainPan.add(datePicker, "wrap");
			} else if (i != DocumentImp.COLUMN_COUNT - 1) {
				JTextField textField = new JTextField(20);
				components[i] = textField;
				mainPan.add(textField, "wrap");
			} else {

				JButton selectFile = new JButton("...");
				final JTextField textField = new JTextField(20);
				selectFile.addActionListener(new ActionListener() {

					public void actionPerformed(ActionEvent arg0) {
						JFileChooser fileChooser;
						if (textField.getText() != null) {
							fileChooser = new JFileChooser(new File(textField
									.getText()));
						} else {
							fileChooser = new JFileChooser();
						}
						int returnVal = fileChooser
								.showOpenDialog(AddFrame.this);

						if (returnVal == JFileChooser.APPROVE_OPTION) {
							File file = fileChooser.getSelectedFile();
							textField.setText(file.getAbsolutePath());
							// This is where a real application would open the
							// file.
						} else {
						}
					}
				});

				mainPan.add(textField);
				components[i] = textField;
				mainPan.add(selectFile, "wrap");
			}

		}
		JButton createButton = new JButton("Create");
		createButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				DocumentImp doc = AddFrame.this.createDocument();
				docDao.addDocument(doc);
				AddFrame.this.setVisible(false);
				docModel.fireTableDataChanged();
				
			}
		});
		mainPan.add(createButton);
		this.setContentPane(mainPan);
		/*
		 * mainPan.add(new JButton("b1")); mainPan.add(new
		 * JButton("b2"),"span 2"); mainPan.add(new JButton("b3"),"wrap");
		 * mainPan.add(new JButton("b4"),"span");
		 */
		/* Creation du JXTable avec son Model */
	}

	public DocumentImp createDocument() {
	
	//	DocumentImp doc = new DocumentImp(new File());
		/*
		doc.setName(((JTextField) components[0]).getText());
		doc.setDescription(((JTextField) components[1]).getText());
		doc.setDateCreation(((JXDatePicker) components[2]).getDate());
		doc.setType(((JTextField) components[3]).getText());
		doc.setProp1(((JTextField) components[4]).getText());
		doc.setProp2(((JTextField) components[5]).getText());
		doc.setProp3(((JTextField) components[6]).getText());
		doc.setContent(((JTextField) components[7]).getText());
		*/
		return null;
	}
}
